package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Concern;
import com.cx.pojo.Post;
import com.cx.pojo.Recommend;
import com.cx.pojo.User;
import com.cx.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("usercenter")
@SessionAttributes(names = {"username"},types = {User.class})
public class UserCenterController {

    @Autowired
    UserCenterService userCenterService;

    @RequestMapping("a")
    public String userCenterJsp(Model model,Integer id){
        System.out.println("a");
        User user = userCenterService.selectByPrimaryId(id);
        model.addAttribute("userId", user.getUserId());
        model.addAttribute("user",user);
        return "forward:/4_user/a.jsp";
    }

/**
 * 个人中心页面-》先登录-》查看个人中心
 * 登录时会将用户的个人信息保存到session中
 * 根据用户的id去查询用户信息，从session中获取用户id在进行查询
 * 而不是作为参数传入进来
 *
 * http://localhost:8888/forum/usercenter/c
 */
    @RequestMapping("c")
    public ModelAndView selectByEmpId(ModelAndView mav, HttpSession session){
       // String userId = (String) session.getAttribute(ConstantData.SESSION_KEY);

//        Integer id = Integer.parseInt(userId);
        Integer id =1;

        User user = userCenterService.selectByPrimaryId(id);

        mav.setViewName("forward:/4_user/a.jsp");
        // 向模型中添加数据-》request.setAttribute(key,value)
        mav.addObject("user",user);

        return mav;
    }

//    个人中心显示个人信息
    @RequestMapping("selectByUserId")
    @ResponseBody
    public User selectByUserId(Integer id){
        User user = userCenterService.selectByPrimaryId(id);
        return user;
    }

//    后台显示所有用户
    @RequestMapping("selectAllUser")
    @ResponseBody
    public User selectAllUser(){

        return null;
    }


@RequestMapping("update")
@ResponseBody
public String update(String userId, String userName, Integer userQq,String userSex, String userPhone,String userArea){
    System.out.println(userId);
    int id = Integer.valueOf(userId).intValue();

    User user = new User();

    user.setUserName(userName);
    user.setUserSex(userSex);
//    user.setUserBirthday(userBirthday);
    user.setUserQq(userQq);
    user.setUserArea(userArea);
    user.setUserPhone(userPhone);

    Integer rows = userCenterService.updateByUserId(id,user);
    if (rows > 0){
        return "success";
    }else {
        return "fail";
    }
}

@RequestMapping("selectAll")
@ResponseBody
public List<User> selectAll(){
        List<User> userList = userCenterService.selectAll();
        return  userList;
}

@RequestMapping("deleteUser")
@ResponseBody
public String deleteUser(Integer userId){
        Integer rows = userCenterService.deleteUser(userId);
        if(rows > 0){
            return "success";
        }else {
            return "fail";
        }
}


    @RequestMapping("selectByUsetName")
    @ResponseBody
    public User selectByUsetName(String userName){
       User user = userCenterService.selectByUsetName(userName);
       return user;
}

@RequestMapping("user")
    @ResponseBody
    public String user(String userName,Model model){
        User user = userCenterService.selectByUsetName(userName);
        model.addAttribute("username1", user.getUserName());
        model.addAttribute("userinformation", user);
        return "success";
}


    @RequestMapping("updateUserByUserName")
    @ResponseBody
    public String updateUserByUserName(String userName, String userPassword, MultipartFile userPhoto,String userSex,String userArea,String other1,String userQq,String userPhone) {
        System.out.println(userName);
//        if (userQq == null || userQq.isEmpty()){
//            System.out.println(123);
//
//        }

        User user = userCenterService.selectByUsetName(userName);

        if (userPhoto == null || userPhoto.isEmpty()){
//            String photo = "http://localhost:8080/img/1.png";
//            http://localhost:8080/img/7cba8e13-bfc2-478e-a296-27084678296c.jpg
            user.setUserPhoto(user.getUserPhoto());

        }else {
        String fileName = userPhoto.getOriginalFilename();
        String name = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        File dest = new File(name);
            try {
                userPhoto.transferTo(dest);// 完成文件上传到指定目录
            } catch (IOException e) {
                e.printStackTrace();
            }
            String userImg = "http://localhost:8080/img/" + name;
            user.setUserPhoto(userImg);


        }
        user.setUserPassword(userPassword);
        user.setUserSex(userSex);
        user.setUserArea(userArea);
        user.setOther1(other1);
        user.setUserQq(Integer.valueOf(userQq));
        user.setUserPhone(userPhone);

        Integer rows = userCenterService.updateUserByUserName(userName,user);
            if (rows > 0){
                return "success";
            }else {
                return "fail";
            }

//        String userName, String userPassword, MultipartFile userPhoto,String userSex,String userArea,String userBirthday,String userQq,String userPhone
//        System.out.println("userPhoto:" + user.getUserPassword());
//        System.out.println("userBirthday:" + user.getUserName());
//        System.out.println("userQq:" + userQq);

        // 头像上传
//        String fileName = userPhoto.getOriginalFilename();
//        String name = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
//        File dest = new File(name);
//        try {
//            userPhoto.transferTo(dest);// 完成文件上传到指定目录
//            String userImg = "http://localhost:8080/img/" + name;
//
//            User user = userCenterService.selectByUsetName(userName);
//            user.setUserPassword(userPassword);
//            user.setUserPhoto(userImg);
//            user.setUserSex(userSex);
//            user.setUserArea(userArea);
//
////            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
////            Date date = simpleDateFormat.parse(userBirthday);
////            user.setUserBirthday(date);
//            user.setOther1(userBirthday);
//            user.setUserQq(Integer.valueOf(userQq));
//            user.setUserPhone(userPhone);
//
//            Integer rows = userCenterService.updateUserByUserName(userName,user);
//            if (rows > 0){
//                return "success";
//            }else {
//                return "fail";
//            }
//        } catch (Exception e ) {
//            e.printStackTrace();
//            return "fail";
//        }
    }



}
